package com.example.joblinker.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.joblinker.entity.UserTodo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户待办事项Mapper接口
 */
@Mapper
public interface UserTodoMapper extends BaseMapper<UserTodo> {

    /**
     * 获取用户待办事项列表
     *
     * @param userId 用户ID
     * @param status 状态(可选)
     * @return 待办事项列表
     */
    @Select("<script>" +
            "SELECT * FROM user_todos " +
            "WHERE user_id = #{userId} " +
            "<if test='status != null and status != \"\"'>" +
            "   AND status = #{status} " +
            "</if>" +
            "ORDER BY urgent DESC, create_time DESC" +
            "</script>")
    List<UserTodo> findByUserIdAndStatus(@Param("userId") Long userId, @Param("status") String status);

    /**
     * 分页获取用户待办事项
     *
     * @param page 分页参数
     * @param userId 用户ID
     * @param status 状态(可选)
     * @return 待办事项分页结果
     */
    @Select("<script>" +
            "SELECT * FROM user_todos " +
            "WHERE user_id = #{userId} " +
            "<if test='status != null and status != \"\"'>" +
            "   AND status = #{status} " +
            "</if>" +
            "ORDER BY urgent DESC, create_time DESC" +
            "</script>")
    Page<UserTodo> pageByUserIdAndStatus(Page<?> page, @Param("userId") Long userId, @Param("status") String status);

    /**
     * 统计用户待办事项数量
     *
     * @param userId 用户ID
     * @param status 状态(可选)
     * @return 待办事项数量
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM user_todos " +
            "WHERE user_id = #{userId} " +
            "<if test='status != null and status != \"\"'>" +
            "   AND status = #{status} " +
            "</if>" +
            "</script>")
    Integer countByUserIdAndStatus(@Param("userId") Long userId, @Param("status") String status);
} 